package com.wentao.mall.entity

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import javax.persistence.AttributeConverter
import javax.persistence.Converter

@Converter
class ListConverter : AttributeConverter<List<Any>, String> {
    private val mapper = ObjectMapper()

    override fun convertToDatabaseColumn(list: List<Any>?): String? {
        return list?.let { mapper.writeValueAsString(list) }
    }

    override fun convertToEntityAttribute(string: String?): List<Any> {
        return mapper.readValue<List<Any>>(string ?: "[]")
    }
}